#ifndef __THREAD__HPP__
#define __THREAD__HPP__

#include <iostream>
#include <string>
#include <functional>
#include <cstdio>
#include <cstring>
#include <pthread.h>
#include "log.hpp"

namespace MyThread
{
    using namespace MyLog;
    static uint32_t cnt = 1;
    class Thread
    {
    private:
        using func_t = std::function<void()>;
        static void* routine(void* args)
        {
            // 静态成员变量没有this指针，无法访问类内成员
            Thread* self = static_cast<Thread*>(args);
            pthread_setname_np(self->_tid, self->_name.c_str());
            self->_func();
            return nullptr;
        }
    public:

        // 创建线程
        Thread(func_t func)
            :_isDetach(false)
            ,_isRunning(true)
            ,_func(func)
        {
            _name = "thread-" + std::to_string(cnt++);
            int n = pthread_create(&_tid, nullptr, routine, (void*)this);
            LOG(loglevel::INFO) << "create new" <<_name << "success";
           // if(n != 0){
           //     std::cerr << "create thread error" << strerror(n) << std::endl;
           //     return;
           // }
           // std::cout << "create thread success!" << std::endl;
        }

        ~Thread(){}
        
        // 回收线程
        bool Join()
        {
            // Join线程时，线程必须是joinable
            if(_isDetach){
                //std::cout << "thread is detached, can not be join" << std::endl;
                return false;
            }
            int n = pthread_join(_tid, nullptr);
            if(n != 0){
                //std::cerr << "join thread error" << strerror(n) << std::endl;
                return false;
            }
            //std::cout << "join thread success!" << std::endl;
            LOG(loglevel::INFO) << "thread->" << _name << "joined";
            return true;
        }

        // 终止线程
        bool Stop()
        {
            if(_isRunning){
                int n = pthread_cancel(_tid);
                if(n != 0){
                    //std::cerr << "stop thread error" << strerror(n) << std::endl;
                    return false;
                }
                //std::cout << "stop thread success!" << std::endl;
                _isRunning = false;
                return true;
            }
            //std::cout << "thread has stopped" << std::endl;
            return false;
        }

        // 分离线程
        bool Detach()
        {
            if(_isDetach){
                //std::cout << "thread is detached" << std::endl;
                return false;
            }
            int n = pthread_detach(_tid);
            if(n != 0){
                //std::cerr << "detach thread error" << std::strerror(n) << std::endl;
                return false;
            }
            //std::cout << "detach thread success!" << std::endl;
            _isDetach = true;
            return true;
        }

        pid_t PID() { return _tid; }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isDetach;
        bool _isRunning;
        func_t _func;
    };

}

#endif